package com.jyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyh.common.PublicField;
import com.jyh.common.R;
import com.jyh.pojo.Orders;
import com.jyh.service.OrdersService;
import com.jyh.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info(orders.toString());
        ordersService.submit(orders);
        return R.success("success");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        //log.info("page:{}",page);
        //log.info("pageSize:{}",pageSize);
        //当前用户
        Long currentId = PublicField.getCurrentId();

        Page<Orders> pageInfo=new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,currentId);
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }
}
